<?php
namespace Core\Event\Dispatch;
use Phalcon\Di\Injectable;
use Core\Application;

class Exception extends Injectable
{
    /**
     * Before exception is happening.
     * @param AbstractEvent            $event      Event object.
     * @param Dispatcher       $dispatcher Dispatcher object.
     * @param PhalconException $exception  Exception object.
     * @return bool
     */
    public function beforeException($event, $dispatcher, $exception) {
        if($this->config->system->debug) {
            return true;
        }
        $systemDefaultModule = ucfirst(Application::SYSTEM_DEFAULT_MODULE);
        $forward = [
            'module' => $systemDefaultModule,
            'namespace' => "{$systemDefaultModule}\\Controller",
            'controller' => 'Error'
        ];
        if ($exception instanceof DispatchException) {
            // Handle 404 exceptions.
            $forward['action'] = "show404";
        }else{
            // Handle other exceptions.
            $forward['action'] = "show500";
        }
        $dispatcher->forward($forward);
        return false;
    }
}